/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __PONY_VFS_FATFS_H__
#define __PONY_VFS_FATFS_H__

/* includes (standard library, system) ---------------------------------------*/
#include <stdint.h>
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
/* includes (local) ----------------------------------------------------------*/
#include "ff.h"
#include "diskio.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/

#define DISK_STATE_INITIALIZED 1

/* typedefs ------------------------------------------------------------------*/

typedef struct disk_dev disk_dev_t;

struct disk_ops {
    DSTATUS (*initialize)(disk_dev_t *dev);                       /*!< Initialize Disk Drive  */
    DSTATUS (*status)(disk_dev_t *dev);                           /*!< Get Disk Status        */
    DRESULT (*read)(disk_dev_t *dev, BYTE *, DWORD, UINT);        /*!< Read Sector(s)         */
    DRESULT (*write)(disk_dev_t *dev, const BYTE *, DWORD, UINT); /*!< Write Sector(s)        */
    DRESULT (*ioctl)(disk_dev_t *dev, BYTE, void *);              /*!< I/O control operation  */
};

struct disk_dev {
    uint8_t state;
    uint8_t lun;
    const struct disk_ops *ops;
    uintptr_t priv; /* private data */
};

/* disk management */
struct disk_man {
    struct disk_dev dev[FF_VOLUMES];
    volatile uint8_t num;
};

/* macro ---------------------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

int fatfs_init(void);
int fatfs_exit(void);
int fatfs_mount(const char *path, struct disk_ops *ops, uintptr_t priv, uint8_t *drive);
int fatfs_unmount(const char *path, uint8_t drive);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __PONY_VFS_FATFS_H__ */
